package org.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class test {

	
	public static void main(String[] args) {

		for (int s = 1; s <= 12; s++) {

			int[] rgb = new int[3];
			ArrayList list1 = new ArrayList();
			ArrayList list2 = new ArrayList();
			ArrayList list3 = new ArrayList();
			ArrayList list4 = new ArrayList();
			int color = 0x0000ff;

			try {
				// 读取图片
				File fileBG = new File(
						"F:\\games\\o2jam\\金星木星\\木星登陆器v1.1\\木星登陆器v1.1\\Image\\SS010002\\Frame"
								+ s + ".bmp");
				
				BufferedImage ImageBG = ImageIO.read(fileBG);

				int BGwidth = ImageBG.getWidth();// 图片宽度
				int BGheight = ImageBG.getHeight();// 图片高度

				int minx = ImageBG.getMinX();
				int miny = ImageBG.getMinY();

				// 读取RGB
				int[] ImageArrayBG = new int[BGwidth * BGheight];
				ImageArrayBG = ImageBG.getRGB(0, 0, BGwidth, BGheight,
						ImageArrayBG, 0, BGwidth);

				for (int i = minx; i < BGwidth; i++) {
					for (int j = miny; j < BGheight; j++) {

						int pixel = ImageBG.getRGB(i, j);
						rgb[0] = (pixel & 0xff0000) >> 16;
						rgb[1] = (pixel & 0xff00) >> 8;
						rgb[2] = (pixel & 0xff);

						// rgb 中的 b 大于240
				if (rgb[2] > 200&&rgb[0]<=70&rgb[0]<=70) {	
					ImageBG.setRGB(i, j, color);
				}

					

						// 如果RGB等于背景色 蓝色
						if (rgb[0] == 0 && rgb[1] == 0 && rgb[2] == 255) {
							// 检测图片上下左右的RGB 四种情况 i+1 i-1 j+1 j-1
							if (i < BGwidth - 1) {
								int test1 = ImageBG.getRGB(i + 1, j);
								rgb[0] = (test1 & 0xff0000) >> 16;
								rgb[1] = (test1 & 0xff00) >> 8;
								rgb[2] = (test1 & 0xff);
								if (rgb[0] != 0 && rgb[1] != 0 && rgb[2] != 255) {
									// 将需要改的颜色坐标记录
									list1.add(String.valueOf(i + 1) + " "
											+ String.valueOf(j));

									// ImageBG.setRGB(i+1, j, color);
								}
							}
							// i-1的情况
							if (i >= 1) {
								int test2 = ImageBG.getRGB(i - 1, j);
								rgb[0] = (test2 & 0xff0000) >> 16;
								rgb[1] = (test2 & 0xff00) >> 8;
								rgb[2] = (test2 & 0xff);
								if (rgb[0] != 0 && rgb[1] != 0 && rgb[2] != 255) {
									list2.add(String.valueOf(i - 1) + " "
											+ String.valueOf(j));

									// ImageBG.setRGB(i-1, j, color);
								}
							}
							// j+1的情况
							if (j < BGheight - 1) {
								int test3 = ImageBG.getRGB(i, j + 1);
								rgb[0] = (test3 & 0xff0000) >> 16;
								rgb[1] = (test3 & 0xff00) >> 8;
								rgb[2] = (test3 & 0xff);
								if (rgb[0] != 0 && rgb[1] != 0 && rgb[2] != 255) {
									list3.add(String.valueOf(i) + " "
											+ String.valueOf(j + 1));

									// ImageBG.setRGB(i, j+1, color);
								}
							}

							// j-1的情况
							if (j >= 1) {
								int test4 = ImageBG.getRGB(i, j - 1);
								rgb[0] = (test4 & 0xff0000) >> 16;
								rgb[1] = (test4 & 0xff00) >> 8;
								rgb[2] = (test4 & 0xff);
								if (rgb[0] != 0 && rgb[1] != 0 && rgb[2] != 255) {
									list4.add(String.valueOf(i) + " "
											+ String.valueOf(j - 1));

									// ImageBG.setRGB(i, j-1, color);
								}
							}
						}
					}
				}

				for (int i = 0; i < list1.size(); i++) {
					String all = list1.get(i).toString();
					String str1[] = all.split(" ");
					int x = Integer.parseInt(str1[0]);
					int y = Integer.parseInt(str1[1]);
					ImageBG.setRGB(x, y, color);

				}
				for (int i = 0; i < list2.size(); i++) {
					String all = list2.get(i).toString();
					String str1[] = all.split(" ");
					int x = Integer.parseInt(str1[0]);
					int y = Integer.parseInt(str1[1]);
					ImageBG.setRGB(x, y, color);

				}
				for (int i = 0; i < list3.size(); i++) {
					String all = list3.get(i).toString();
					String str1[] = all.split(" ");
					int x = Integer.parseInt(str1[0]);
					int y = Integer.parseInt(str1[1]);
					ImageBG.setRGB(x, y, color);

				}
				for (int i = 0; i < list4.size(); i++) {
					String all = list4.get(i).toString();
					String str1[] = all.split(" ");
					int x = Integer.parseInt(str1[0]);
					int y = Integer.parseInt(str1[1]);
					ImageBG.setRGB(x, y, color);

				}

				FileOutputStream ops = new FileOutputStream(new File(
						"F:\\games\\o2jam\\金星木星\\木星登陆器v1.1\\木星登陆器v1.1\\Image\\SS010002\\Frame"
								+ s + ".bmp"));
				System.out.println(s);
				ImageIO.write(ImageBG, "bmp", ops);
				ops.flush();
				ops.close();

			} catch (Exception e) {
				System.out.println(e);
			}

		}

	}

}
